package com.ruoyi.business.controller;

import java.io.IOException;
import java.util.List;

import com.ruoyi.business.domain.JtProduct;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.utils.JtImageUtil;
import com.ruoyi.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.JtBanner;
import com.ruoyi.business.service.IJtBannerService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 横幅广告Controller
 * 
 * @author Alan Zhang
 * @date 2025-03-29
 */
@Controller
@RequestMapping("/product/banner")
public class JtBannerController extends BaseController
{
    private String prefix = "product/banner";

    @Autowired
    private IJtBannerService jtBannerService;

    @RequiresPermissions("product:banner:view")
    @GetMapping()
    public String banner()
    {
        return prefix + "/banner";
    }

    /**
     * 查询横幅广告列表
     */
    @RequiresPermissions("product:banner:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JtBanner jtBanner)
    {
        startPage();
        List<JtBanner> list = jtBannerService.selectJtBannerList(jtBanner);
        return getDataTable(list);
    }

    /**
     * 导出横幅广告列表
     */
    @RequiresPermissions("product:banner:export")
    @Log(title = "横幅广告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JtBanner jtBanner)
    {
        List<JtBanner> list = jtBannerService.selectJtBannerList(jtBanner);
        ExcelUtil<JtBanner> util = new ExcelUtil<JtBanner>(JtBanner.class);
        return util.exportExcel(list, "横幅广告数据");
    }

    /**
     * 新增横幅广告
     */
    @RequiresPermissions("product:banner:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存横幅广告
     */
    @RequiresPermissions("product:banner:add")
    @Log(title = "横幅广告", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult addSave(JtBanner jtBanner, MultipartFile file) throws IOException {
        if(file != null){
            jtBanner.setImageUrl(JtImageUtil.uploadBanner(file, true));
        }
        return toAjax(jtBannerService.insertJtBanner(jtBanner));
    }

    /**
     * 修改横幅广告
     */
    @RequiresPermissions("product:banner:edit")
    @GetMapping("/edit/{bannerId}")
    public String edit(@PathVariable("bannerId") Long bannerId, ModelMap mmap)
    {
        JtBanner jtBanner = jtBannerService.selectJtBannerByBannerId(bannerId);
        mmap.put("jtBanner", jtBanner);
        return prefix + "/edit";
    }

    /**
     * 修改保存横幅广告
     */
    @RequiresPermissions("product:banner:edit")
    @Log(title = "横幅广告", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JtBanner jtBanner, @RequestParam(value = "file", required = false) MultipartFile file) throws IOException {
        if(file != null){
            jtBanner.setImageUrl(JtImageUtil.uploadBanner(file, true));

            //删除源文件
            JtBanner jtBannerDb = jtBannerService.selectJtBannerByBannerId(jtBanner.getBannerId());
            String oldImageUrl = jtBannerDb.getImageUrl();
            if(StringUtils.isEmpty(oldImageUrl)){
                JtImageUtil.deleteFile(oldImageUrl);
            }
        }
        return toAjax(jtBannerService.updateJtBanner(jtBanner));
    }

    /**
     * 删除横幅广告
     */
    @RequiresPermissions("product:banner:remove")
    @Log(title = "横幅广告", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jtBannerService.deleteJtBannerByBannerIds(ids));
    }
}
